var ue = UE.getEditor('content');
$('#content').data('editor', ue);
$('#content').parents('form').submit(function () {
    if (ue.queryCommandState('source')) {
        ue.execCommand('source');
    }
});

UE.registerUI('uploadImage', function (editor, uiName) {
    return new UE.ui.Button({
        name: '上传图片',
        title: '上传图片',
        cssRules: 'background-position:-380px 0',
        onclick: function () {
            layer.open({
                id: 'selectImgs',
                type: 2,
                title: '批量选择图片',
                maxmin: true,
                shadeClose: false,
                area: ['60%', '70%'],
                content: '/platform/config/batchUploadImgUeditor'  // iframe的url
            });
        }
    });
});


function save() {
    if ($("#name").val() == '' || $("#name").val() == undefined) {
        layer.alert("请输入品牌名称!", {icon: 5});
        return false;
    }
    if ($.trim($(':input[name="logo"]').val()) == '') {
        layer.alert("请上传logo!", {icon: 5});
        return false;
    }
    if ($.trim($(':input[name="description"]').val()) == '') {
        layer.alert("请上传品牌背景图!", {icon: 5});
        return false;
    }

    var sort = $("#sort").val();
    if (sort > 2147483647) {
        layer.msg("排序值过大");
        return;
    }

    var name = $("#name").val();
    var logo = $("#logo").val();
    var description = $("#description").val();
    var centreId = $("#centreId").val() != null && $("#centreId").val() != '' ? $("#centreId").val() : $("#centreId").val();
    var content = UE.getEditor('content').getContent();

    var data = {
        "sort": sort,
        "name": name,
        // "teamId": teamId,
        "centreId": centreId,
        "logo": logo,
        "description": description,
        "content": content
    };
    $.ajax({
        cache : true,
        type : "POST",
        url : "/reservationBrandCategory/manage/save",
        // data : $('#addForm').serialize(),// 你的formid
        data: data,
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                if (data.msg) {
                    parent.layer.alert(data.msg)
                }else {
                    window.location.href = "/";
                }
            }

        }
    });

}

function getCentreNmae() {
    $("#centreName").val($("#centreId option:checked").text());
}


/**
 * 选择图片页面
 * @param inputId
 * @param imgId
 */
function onSelectImgBtnClick(inputId, imgId) {
    layer.open({
        id: 'selectImg',
        type: 2,
        title: '选择图片',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '70%'],
        content: '/platform/config/selectImg/' + inputId + "/" + imgId // iframe的url
    });
}

/**
 * 加载图片URL
 * @param inputId
 * @param imgId
 * @param url
 */
function loadImgUrl(inputId, imgId, url) {
    $('#'+inputId +'').val(url);
    $('#'+imgId +'').attr('src', url);
}